package com.cn.mysql.utils

import java.text.SimpleDateFormat
import java.util.Date

/**
 *
 * val sdf=new SimpleDateFormat("yyyyMMdd")
 * //date2String
 * println(sdf.format(new Date()))
 * //string2Date
 * println(sdf.parse("20211226"))
 */

object DateUtils {

  def date2String(date: Date,template: String): String ={
    val sdf = new SimpleDateFormat(template)
    sdf.format(date)
  }

  def string2Date(string: String,template: String): Date ={
    val sdf = new SimpleDateFormat(template)
    sdf.parse(string)
  }

  def main(args: Array[String]): Unit = {
    // 时间格式转字符串
    val date=new Date()
    println(DateUtils.date2String(date,"yyyy-MM-dd"))
    //2021-12-26

    val dateStr="2021-12-26"
    val template="yyyy-MM-dd"
    println(DateUtils.string2Date(dateStr,template))
    // Sun Dec 26 00:00:00 CST 2021
  }
}
